# coding=utf-8
from abc import ABCMeta
import tornado.web
import tornado.escape
import json

from CommonLib import CheckJsonKeys, CheckAuth
from database.models.DatabaseCommon import *
from database.models.Normal import Normal
import WrapCursor

class DelNormalFlowHandler(tornado.web.RequestHandler):
    __metaclass__ = ABCMeta

    def post(self):
        r = WrapCursor.WrapCursor()
        try:
            ojson = json.loads(self.request.body)
            desc = ojson['mode_id']
            CheckJsonKeys(ojson, 'username', 'userpass')
            CheckAuth(ojson)
        except Exception as e:
            self.write(r.Error('request data error' + e.message))
            return
        nid = ojson['mode_id']
        if nid == "*":
            self.write(r.Error(u'要删除的模式不存在'))
            return
        else:

            s = Session()
            try:
                ns = s.query(Normal).filter_by(id=nid).all()
                for i in ns:
                    s.delete(i)
                s.commit()
            except:
                self.write(r.Error(u'失败'))
                return
        jsonret = {"Code": 0, "desc": u'成功'}
        self.write(json.dumps(jsonret))